<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Starpt_model extends CI_Model {
	
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * 글 리스트
	 */
	function select_starpt_list($search_type, $search_keyword, $page_no, $page_size, $user_id = NULL) {
		log_message('debug',"#### Starpt_model::select_starpt_list($search_type, $search_keyword, $page_no, $page_size, $user_id )");
		
		$start_row = ($page_no-1) * $page_size;

		$this->db->where('category' , 'starpt');
		if (!empty($user_id)) {
			$this->db->where('user_id', $user_id);
		}
		if (!empty($search_keyword)) {
			$this->db->like($search_type, $search_keyword);
		}
		$this->db->order_by('create_dt', 'desc');
		$this->db->limit($page_size, $start_row);
		$query = $this->db->get('tb_bbs') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		for ($i=0; $i < count($result); $i++) { 
			$result[$i]['file'] = $this->select_bbs_file_list($result[$i]['idx']);
		}

		return $result;
	}

	/**
	 * 총 내역 수
	 */
	function get_starpt_total_count($search_type, $search_keyword) {
		log_message('debug',"#### Starpt_model::get_starpt_total_count($search_type, $search_keyword)");

		$this->db->where('category' , 'starpt');
		if (!empty($search_keyword)) {
			$this->db->like($search_type, $search_keyword);
		}
		$this->db->from('tb_bbs');
		$result = $this->db->count_all_results();

		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 글 저장
	 */
	function save_starpt($bbs_content, $result_upload) {
		log_message('debug',"#### Starpt_model::save_starpt($bbs_content, $result_upload)");
		
		$this->db->trans_start();
		
		$this->insert_bbs($bbs_content);

		foreach ($result_upload as $item) {
			$this->insert_bbs_file($item);
		}
		
		
		$this->db->trans_complete();
	}

	/**
	 * 글 내용 저장
	 */
	function insert_bbs($bbs_content) {
		log_message('debug',"#### Starpt_model::insert_bbs($bbs_content)");

		$this->db->set('category'  , $bbs_content['category']);
		$this->db->set('user_id'   , $bbs_content['user_id']);
		$this->db->set('nickname'  , $bbs_content['nickname']);
		$this->db->set('service_id', '');
		$this->db->set('artist_idx', $bbs_content['artist_idx']);
		$this->db->set('subject'   , $bbs_content['subject']);
		$this->db->set('content'   , $bbs_content['content']);
		
		$this->db->set('create_dt' , 'now()', FALSE);
		$this->db->set('update_dt' , 'now()', FALSE);
		$this->db->insert('tb_bbs') or $this->logsql->log_sql('error');

		$this->logsql->log_sql();
	}

	/**
	 * 파일 저장
	 */
	function insert_bbs_file($result_upload) {
		log_message('debug',"#### Bbs_model::insert_bbs_file($result_upload)");

		$this->db->set('idx'           , 'last_insert_id()', FALSE);
		$this->db->set('file_name'     , $result_upload['file_name']);
		$this->db->set('file_type'     , $result_upload['file_type']);
		$this->db->set('file_path'     , $result_upload['file_path']);
		$this->db->set('file_url'      , $result_upload['file_url']);
		$this->db->set('raw_name'      , $result_upload['raw_name']);
		$this->db->set('orig_name'     , $result_upload['orig_name']);
		$this->db->set('file_ext'      , $result_upload['file_ext']);
		$this->db->set('file_size'     , $result_upload['file_size']);
		$this->db->set('is_image'      , $result_upload['is_image'] == 1 ? 'Y' : 'N');
		$this->db->set('image_width'   , '');
		$this->db->set('image_height'  , '');
		$this->db->set('image_type'    , '');
		$this->db->set('image_size_str', '');
		$this->db->insert('tb_bbs_file') or $this->logsql->log_sql('error');
		
		$this->logsql->log_sql();
	}

	/**
	 * 스타 리스트 검색 (키워드)
	 */
	function select_artist_search_list($search_keyword) {
		log_message('debug',"#### Starpt_model::select_artist_search_list($search_keyword)");

		$this->db->select('idx, artist_name, job, birth_yyyy, birth_mm, birth_dd, sex, body, photo');
		$this->db->like('keyword', $search_keyword);
		$query = $this->db->get('tb_artist') or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);

		return $result;
	}

	/**
	 * 파일 리스트
	 */
	function select_bbs_file_list($idx) {
		log_message('debug',"#### Bbs_model::select_bbs_file_list($idx)");
		
		$sql  = PHP_EOL." SELECT F.* ";
		$sql .= PHP_EOL."     , CONCAT(file_url,raw_name,'_SIZE',file_ext) thumb_url ";
		$sql .= PHP_EOL." from tb_bbs_file F ";
		$sql .= PHP_EOL." where F.idx = ? ";
		$query = $this->db->query($sql,array($idx)) or $this->logsql->log_sql('error');
		$result = $query->result_array();
		
		$this->logsql->log_sql();
		$this->logsql->log_result($result);
		
		return $result;
	}


	/**
	 * 글 삭제
	 * 글에 연관된 모든 데이터 삭제
	 * 1. 댓글 삭제
	 * 2. 첨부파일정보 삭제
	 * 3. 글 삭제
	 * 4. 첨부파일 삭제
	 */
	function remove_bbs($idx) {
		log_message('debug',"#### Starpt_model::remove_bbs($idx)");
		
		$this->db->trans_start();

		$files = $this->select_bbs_file_list($idx);

		$tables = array(
			  'tb_bbs_reply'
			, 'tb_bbs_file'
			, 'tb_bbs'
		);
		$this->db->where('idx', $idx);
		$this->db->delete($tables);

		$this->db->trans_complete();
		
		
		if ($this->db->trans_status() == TRUE) {
			// db삭제가 완료되면 첨부파일 삭제
			foreach ($files as $file) {
				$file_full_path = $file['file_path'].$file['file_name'];
				$file_full_path2 = $file['file_path'].$file['raw_name'].'*';
				if(is_file($file_full_path)) {
					//unlink($file_full_path);
					//unlink($file_full_path2);
					`rm -f $file_full_path2`;
					log_message('debug',"## ".$file_full_path."삭제 완료");
				}
			}
			return TRUE;
		}
		return FALSE;
	}
	
}

/* End of file starpt_model.php */
/* Location: ./application/models/admin/starpt_model.php */